package cn.rongdao.jrkabao.ui.creditcard;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.creditcard.CreditCardBalanceBean;
import cn.rongdao.jrkabao.bean.creditcard.ShortMessageBean;
import cn.rongdao.jrkabao.model.creditcard.CreditCardBalanceModel;
import cn.rongdao.jrkabao.model.creditcard.ShortMessageModel;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.ItemCashAdvanceSuccessUIComponent;
import cn.rongdao.jrkabao.widget.SelectDateFromBottom;

/**
 * Created by jack on 2017/9/9
 */

public class CreditCardBalanceActivity extends BaseAutoFinishActivity {

    public static void start(Context context) {
        FRStartActivity.start(context, CreditCardBalanceActivity.class);
    }

    @BindView(R.id.accb_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.accb_bank_name)
    ItemCashAdvanceSuccessUIComponent bankNameUIComponent;
    @BindView(R.id.accb_last_card_number)
    ItemCashAdvanceSuccessUIComponent lastCardNumberUIComponent;
    @BindView(R.id.accb_go_query)
    TextView tvGoQuery;

    private SelectDateFromBottom selectDateFromBottom;

    private String bankName;
    private String bankCode;
    private List<String> bankNameList = new ArrayList<>();
    private List<CreditCardBalanceBean> creditCardBalanceBeanList;

    @Override
    protected int layoutRes() {
        return R.layout.activity_credit_card_balance;
    }

    @Override
    protected void initView() {
        super.initView();
        initTitleBar();
        initListener();
        getCreditData();
        initSelectBank();
    }

    private void initSelectBank() {
        lastCardNumberUIComponent.setCreditRemindCardNumberContent("信用卡尾号", "");
        bankNameUIComponent.setCreditRemindBankContent("所属银行", "");

        selectDateFromBottom = new SelectDateFromBottom(thisActivity());

        selectDateFromBottom.setOnSelectDateListener(new SelectDateFromBottom.OnSelectDateListener() {
            @Override
            public void getSelectDate(boolean isRepayment, final String number) {
                bankName = number;
                bankNameUIComponent.setCreditRemindBankContent("所属银行", number);
                selectDateFromBottom.dismiss();
            }
        });
    }

    private void initListener() {
        tvGoQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发短信
                bankCode = getCodeByBankName();
                if (FRString.isEmpty(lastCardNumberUIComponent.getEditContent()) || FRString.isEmpty(bankCode)) {
                    FRToast.showToastSafe("银行卡或尾号不能为空");
                    return;
                }
                if (lastCardNumberUIComponent.getEditContent().length() < 4) {
                    FRToast.showToastSafe("请输入4位卡尾号");
                    return;
                }
                getSMSTemplate();
            }
        });
        bankNameUIComponent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectDateFromBottom.show();
            }
        });
    }

    private void getSMSTemplate() {
        ShortMessageModel.getInstance().execute(bankCode, lastCardNumberUIComponent.getEditContent(), new FRBaseObserver<ShortMessageBean>(this, "请稍候...") {
            @Override
            protected void onBaseNext(ShortMessageBean data) {
                jumpToShortMessage(data.getNumber(), data.getTemplate());
            }
        });
    }

    private void jumpToShortMessage(String number, String template) {
        if (!FRString.isEmpty(number) && !FRString.isEmpty(template)) {
            Uri smsToUri = Uri.parse(FRCollection.appendAsString("smsto:", number, ""));
            Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
            intent.putExtra("sms_body", template);
            startActivity(intent);
        }
    }


    private void getCreditData() {
        CreditCardBalanceModel.getInstance().execute(new FRBaseObserver<List<CreditCardBalanceBean>>(this) {
            @Override
            protected void onBaseNext(List<CreditCardBalanceBean> data) {
                creditCardBalanceBeanList = data;
                setBankPopupDataList();
            }
        });
    }

    private void setBankPopupDataList() {
        if (null != creditCardBalanceBeanList) {
            for (CreditCardBalanceBean creditCardBalanceBean : creditCardBalanceBeanList) {
                bankNameList.add(creditCardBalanceBean.getBank_name());
            }
            if (bankNameList.size() > 0) {
                selectDateFromBottom.setDataList(bankNameList);
            }
        }
    }

    private String getCodeByBankName() {
        if (FRString.isEmpty()) {
            return FRString.EMPTY_STRING;
        }
        int index = bankNameList.indexOf(bankName);
        if (index < 0 || index > bankNameList.size()) {
            return FRString.EMPTY_STRING;
        }
        return creditCardBalanceBeanList.get(index).getBank_code();
    }

    private void initTitleBar() {
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initTitle("查信用卡余额");
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
